package ejb.manager;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import ejb.dao.CityDAO;
import ejb.dao.ForecastDAO;
import ejb.entity.City;
import ejb.entity.DefaultCity;
import ejb.entity.Forecast;
import ejb.entity.Position;

@Stateless
public class DefaultForecastManager implements ForecastManager {
	@EJB
	private ForecastDAO fcdao;
	@EJB
	private CityDAO citydao;
	@EJB
	private CityManager cityManager;
	
	
	@Override
	public Forecast getForecast(City city, int plusday) {
		return fcdao.findByCityAndDay(city, plusday);
	}
	
	@Override
	public void create(Forecast forecast) {
		fcdao.create(forecast);
	}
	
	@Override	
	public City getPositionForecast(Position position, int plusday){
		List<City> citiesForecast = citydao.getDetachedCities();
	
		for(City c : citiesForecast){
			Forecast forecast = fcdao.findByCityAndDay(c, plusday);
			c.setConditions(forecast.getConditions());
			c.setTemperature(forecast.getTemperature());
		}
		
		int conditions = cityManager.approxConds(position.getPositionx(), position.getPositiony(), citiesForecast);
		float temp = (float) cityManager.approxTemp(position.getPositionx(), position.getPositiony(), citiesForecast);
		
		City approxPosition = new DefaultCity();
		approxPosition.setName(position.getName());
		approxPosition.setConditions(conditions);
		approxPosition.setTemperature(temp);
		return approxPosition;
	}
}
